<template>
  <div class="app-container">
    <el-button
      class="filter-item"
      size="small"
      type="primary"
      icon="el-icon-plus"
      @click="handerCreate()"
    >新增通知
    </el-button>
    <el-card style="margin-top: 15px;">
      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="notices"
        border
        fit
        highlight-current-row
        style="width: 100%;margin-top: 15px;"
      >
        <el-table-column label="ID" prop="id" align="center" width="80" />
        <el-table-column label="通知标题" align="center">
          <template slot-scope="{row}">
            <span>{{ row.title }}</span>
          </template>
        </el-table-column>

        <el-table-column label="创建时间" align="center">
          <template slot-scope="{row}">
            <span>{{ row.createdAt | date_format }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="handleEdit(row)">
              <i class="el-icon-edit" /> 编辑
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(row)">
              <i class="el-icon-delete" /> 删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import '../../filter'
import { deleteNotice, fetchNoticeList } from '@/api/notice'

export default {
  data() {
    return {
      notices: [],
      listLoading: false,
      ruleForm: {
        name: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      fetchNoticeList().then(response => {
        // console.log(response)
        this.notices = response.data.notices
        this.listLoading = false
      })
    },
    // 加载新增页面
    handerCreate() {
      this.$router.push({ name: 'NoticeCreate' })
    },
    // 跳转到编辑页面
    handleEdit(row) {
      this.$router.push({
        name: 'NoticeEdit',
        params: { id: row.id }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确认要删除该通知吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        deleteNotice(row.id).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: res.message
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
